package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Objects;

@RestController
public class LoginController {

    @Autowired
    UserDao userDao;
    @RequestMapping("/login")
    public String loginuser(@RequestBody User user){
        System.out.println("收到登录请求：" + user.getUsername() + ", " + user.getPassword());
        String flag="error";
        User us=userDao.getUsermessage(user.getUsername(),user.getPassword());
        HashMap<String, Object> hashMap=new HashMap<>();
        if(us!=null){
            flag="ok";

        }
        hashMap.put("flag",flag);
        hashMap.put("user",us);
        System.out.println(us);
        String res_Json= JSON.toJSONString(hashMap);
      return res_Json;
    }
    @PostMapping("/register")
    public String registeruser(@RequestBody User user){
        int req=userDao.registeruser(user.getUsername(),user.getPassword());
        return req > 0 ? "success" : "error";
    }
}